<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Goods;
use app\common\model\GoodsAttr;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 分类列表
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1605535963",
    "data": [{
    "id": 3,
    "name": "卡通蛋糕",
    "createtime": 1605255674
    }, {
    "id": 2,
    "name": "水果蛋糕",
    "createtime": 1605255674
    }, {
    "id": 1,
    "name": "经典蛋糕",
    "createtime": 1605255674
    }]
    })
     */
    public function category_list()
    {
        $category_list = Category::order('id desc')->select();
        $this->success($category_list);
    }
    /**
     * 商品列表
     * @param string $category_id  分类ID
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1605536164",
    "data": [{
    "id": 6,商品ID
    "name": "天然零卡糖",名称
    "image": "http:\/\/www.cake.com\/uploads\/20201116\/5268b1414a12bbad4cfb204bc97dd3dd.png",图片
    "price": "78.00"价格
    }]
    })
     */
    public function goods_list()
    {
        $category_id = $this->request->get('category_id');
        $list = Goods::where('category_id',$category_id)->order('id desc')->field('id,name,image,price')->select();
        foreach($list as &$value){
            $value['image'] = $this->domain.$value['image'];
        }
        $this->success($list);
    }
    /**
     * 商品规格列表
     * @param string $goods_id  商品ID
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1605536326",
    "data": [{
    "id": 13,规格ID
    "goods_id": 6,商品ID
    "attr_name": "4寸",规格名称
    "price": "78.00",价格
    "share_num": "适合3-4人分享",分享数量
    "tableware_num": "含4套餐具",餐具数量
    "createtime": 1605516649
    }, {
    "id": 14,
    "goods_id": 6,
    "attr_name": "8寸",
    "price": "128.00",
    "share_num": "适合5-6人分享",
    "tableware_num": "含6套餐具",
    "createtime": 1605516881
    }]
    })
     */
    public function goods_attr()
    {
        $goods_id = $this->request->get('goods_id');
        $list = GoodsAttr::where('goods_id',$goods_id)->order('price asc')->select();
        $this->success($list);
    }
    /**
     * 商品详情
     * @param string $goods_id  商品ID
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1605578594",
    "data": {
    "id": 6,商品名称
    "category_id": 2,
    "image": "http://www.cake.com/uploads/20201116/5268b1414a12bbad4cfb204bc97dd3dd.png",图片
    "name": "天然零卡糖",
    "createtime": 1605516649,
    "content": "<p>天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖天然零卡糖<img src=\"http://www.cake.com/uploads/20201116/b3096f7a71bfd6a48871aaffd627bbce.png\" ><br></p>",详情
    "updatetime": 1605577230,
    "price": "78.00",价格
    "sweetness": 2甜度
    }
    })
     */
    public function goods_detail()
    {
        $goods_id = $this->request->get('goods_id');
        $info = Goods::where('id',$goods_id)->find();
        if($info){
            $info['image'] = $this->domain.$info['image'];
            $info['content'] = get_img_thumb_url($info['content'],$this->domain);
        }
        $this->success($info);
    }
}
